local Log = {}

local cjson = require "cjson"

local log_file = "/data/mockdata/mock_log"

LOG_ERROR = 0
LOG_DEBUG = 1

local default_log_level = LOG_ERROR

function Log:write_file(data)
    local file = io.open(log_file, "a+")
    if file == nil then
        return
    end
    file:write(data)
    file:close()
end

function Log:print(level, fmt, ...)
    local cur_time = os.date("%c")
    local json = cjson.encode(string.format(fmt, ...))
    local info = debug.getinfo(2)
    local chip_name = string.match(info.short_src, "[%a]+%.lua") or "unknown"
    local line_num = info.currentline
    local str = "["..cur_time.."]".."["..FileName.."]".."("..chip_name..")".."("..line_num..")"..json.."\n"
    if default_log_level >= level then
        self:write_file(str)
    end
    if default_log_level == LOG_DEBUG then
        print(str)
    end
end

return Log